package agents;

import constants.*;

import interfaces.ActionInterface;
import interfaces.AgentInterface;
import interfaces.impl.Agent;
import interfaces.impl.actions.ActionModifySpetialFeature;

import java.awt.Graphics;
import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionGenerateObject;

public class AgentEnemyCastle extends Agent implements AgentInterface{
	
	private ActionGenerateObject actionGenerateObject;
	private ActionModifySpetialFeature actionModifySpetialFeature;
	
	public AgentEnemyCastle(){
		this.id=Constants.AGENT_ENEMY_CASTLE;
		
		listActions= new ArrayList<ActionInterface>();
		
		actionGenerateObject=new ActionGenerateObject();
		listActions.add(actionGenerateObject);
		
		actionModifySpetialFeature= new ActionModifySpetialFeature();
		listActions.add(actionModifySpetialFeature);
		
	}

	@Override
	public void update(WorldObject object, Universe worldI, double time) {
		if(!canUpdate(object)){
			System.out.println("Me falta alguna accion para poder actuar");
			return;
		}
		SpetialFeature money= new SpetialFeature();
		WorldObject tgt = new WorldObject();
		int cont=0;
		for(WorldObject obj : object.getListObjects()){
			if(obj.getObjectReference()==Constants.OBJECT_REFERENCE_MONEY){
				for(SpetialFeature fe : obj.getListFeatures()){
					if(fe.getId()==Constants.SPETIAL_FEATURE_MONEY){
						money=fe;
						cont++;
					}
				}
				if(cont!=1){return;}
				else{
					tgt=obj;
					break;
					}
			}
			
		}
		
		
		
		//System.out.println("actualizo Enemy castle");
		
		if(money.getValue()>50){
			actionGenerateObject.setTarget(object);
			actionGenerateObject.setSource(object);
			actionGenerateObject.setObjectReference(Constants.OBJECT_REFERENCE_FUNGI);
			actionGenerateObject.setX(object.getPosition().getX());
			actionGenerateObject.setY(object.getPosition().getY());
			
			object.setImmediatlyActionToDo(actionGenerateObject);
			
			actionModifySpetialFeature.setTarget(tgt);
			actionModifySpetialFeature.setSource(object);
			actionModifySpetialFeature.setValue(money.getValue()-40);
			actionModifySpetialFeature.setFeatureId(Constants.SPETIAL_FEATURE_MONEY);
			object.setImmediatlyActionToDo(actionModifySpetialFeature);
			
		}
		
	}

	@Override
	public void paint(Graphics g, WorldObject object) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Agent Enemy Castle";
	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub
		
	}
	

}
